package com.wenhui.schoolyardinfo.login.presenter;

import android.content.Context;

import com.wenhui.schoolyardinfo.login.callback.OnLoginListener;
import com.wenhui.schoolyardinfo.login.model.LoginModelImpl;
import com.wenhui.schoolyardinfo.login.model.base.ILoginModel;
import com.wenhui.schoolyardinfo.login.presenter.base.BasePresenter;
import com.wenhui.schoolyardinfo.login.presenter.base.ILoginPresenter;
import com.wenhui.schoolyardinfo.login.view.IForgetPasswordView;
import com.wenhui.schoolyardinfo.login.view.ILoginView;
import com.wenhui.schoolyardinfo.login.view.IRegisterView;
import com.wenhui.schoolyardinfo.login.view.base.IBaseView;

import java.util.Map;

/**
 * author    : Urasaki
 * e-mail    : 1164688204@qq.com
 * date      : 2019/7/9 17:04
 * introduce : Presenter登录模块的（一个小模块对应一个Presenter）
 */

public class LoginPresenterImpl extends BasePresenter<IBaseView> implements ILoginPresenter {

    private static final String TAG = "MinePresenterImpl";
    //    private IMineView loginView;//P需要与V 交互，所以需要持有V的引用
    private ILoginModel loginModel;

    public LoginPresenterImpl(Context context, IBaseView baseView) {
        attachView(baseView);
        loginModel = new LoginModelImpl(context);
    }

    @Override
    public void login(Map<String, String> bodyParams) {
        if (getView() != null) {
            if (getView() instanceof ILoginView) {
                final ILoginView loginView = (ILoginView) getView();
                loginView.showLoading();
                loginModel.login(bodyParams, new OnLoginListener() {
                    @Override
                    public void onLoginSuccess(String success) {
                        loginView.loginSuccess(success);
                        loginView.hideLoading();
                    }

                    @Override
                    public void onLoginError(String error) {
                        loginView.loginError(error);
                        loginView.hideLoading();
                    }
                });
            }
        }

    }

    @Override
    public void register(Map<String, String> bodyParams) {
        if (getView() != null) {
            if (getView() instanceof IRegisterView) {
                final IRegisterView registerView = (IRegisterView) getView();
                registerView.showLoading();
//                loginModel.register(bodyParams, new OnLoginListener() {
//                    @Override
//                    public void onLoginSuccess(String success) {
//                        registerView.registerSuccess(success);
//                    }
//
//                    @Override
//                    public void onLoginError(String error) {
//                        registerView.registerError(error);
//                    }
//                });
            }
        }

    }

    @Override
    public void changePassword(Map<String, String> bodyParams) {
        if (getView() != null) {
            if (getView() instanceof IForgetPasswordView) {
                final IForgetPasswordView forgetPasswordView = (IForgetPasswordView) getView();
                forgetPasswordView.showLoading();
                loginModel.changePassword(bodyParams, new OnLoginListener() {
                    @Override
                    public void onLoginSuccess(String success) {
                        forgetPasswordView.changePasswordSuccess(success);
                    }

                    @Override
                    public void onLoginError(String error) {
                        forgetPasswordView.changePasswordError(error);
                    }
                });
            }
        }

    }
}
